home *** CD-ROM | disk | FTP | other *** search
/ Software 2000 / Software 2000 Volume 1 (Disc 1 of 2).iso / utilities / u149.dms / u149.adf / Input / Joystick.c < prev    next >
C/C++ Source or Header  |  1991-05-25  |  2KB  |  104 lines

  1. /* Joystick()                                                          */
  2. /* Joystick() is a handy, easy and fast but naughty function that hits */
  3. /* the hardware of the Amiga. It looks at either port 1 or port 2, and */
  4. /* returns a bitfield containing the position of the stick and the     */
  5. /* present state of the button.                                        */
  6. /*                                                                     */
  7. /* Synopsis: value = Joystick( port );                                 */
  8. /* value:    (UBYTE) If the fire button is pressed, the first bit is   */
  9. /*           set. If the stick is moved to the right, the second bit   */
  10. /*           is set, and if the stick is moved to the left, the third  */
  11. /*           bit is set. The fourth bit is set if the stick is moved   */
  12. /*           down, and the fifth bit is set if the stick is moved up.  */
  13. /* port:     (UBYTE) Set the flag PORT1 if you want to check the first */
  14. /*           (mouse) port, or set the flag PORT2 if you want to check  */
  15. /*           the second (joystick) port.                               */
  16.  
  17.  
  18. #include <exec/types.h>
  19. #include <hardware/custom.h>
  20. #include <hardware/cia.h>
  21.  
  22.  
  23. #define CIAAPRA 0xBFE001 
  24.  
  25. #define FIRE   1
  26. #define RIGHT  2
  27. #define LEFT   4
  28. #define DOWN   8
  29. #define UP    16
  30.  
  31. #define PORT1 1
  32. #define PORT2 2
  33.  
  34.  
  35. extern struct Custom far custom;
  36. struct CIA *cia = (struct CIA *) CIAAPRA;
  37.  
  38.  
  39. void main();
  40. UBYTE Joystick();
  41.  
  42.  
  43. void main()
  44. {
  45.   int timer = 0;
  46.   UBYTE value = 0;
  47.   UBYTE old_value = 0;
  48.   
  49.   
  50.   while( timer < 30 )
  51.   {
  52.     old_value = value;
  53.  
  54.     value = Joystick( PORT2 );
  55.     
  56.     if( value != old_value )
  57.     {
  58.       timer++;
  59.  
  60.       if( value & FIRE )
  61.         printf("FIRE ");
  62.       if( value & RIGHT )
  63.         printf("RIGHT ");
  64.       if( value & LEFT )
  65.         printf("LEFT ");
  66.       if( value & DOWN )
  67.         printf("DOWN ");
  68.       if( value & UP )
  69.         printf("UP ");
  70.       printf("\n");
  71.     }
  72.   }
  73. }
  74.  
  75.  
  76.  
  77. UBYTE Joystick( port )
  78. UBYTE port;
  79. {
  80.   UBYTE data = 0;
  81.   UWORD joy;
  82.   
  83.   if( port == PORT1 )
  84.   {
  85.     /* PORT 1 ("MOUSE PORT") */
  86.     joy = custom.joy0dat;
  87.     data += !( cia->ciapra & 0x0040 ) ? FIRE : 0;
  88.   }
  89.   else
  90.   {
  91.     /* PORT 2 ("JOYSTICK PORT") */
  92.     joy = custom.joy1dat;
  93.     data += !( cia->ciapra & 0x0080 ) ? FIRE : 0;
  94.   }
  95.  
  96.   data += joy & 0x0002 ? RIGHT : 0;
  97.   data += joy & 0x0200 ? LEFT : 0;
  98.   data += (joy >> 1 ^ joy) & 0x0001 ? DOWN : 0;
  99.   data += (joy >> 1 ^ joy) & 0x0100 ? UP : 0;
  100.  
  101.   return( data );
  102. }
  103.  
  104.